#include "faster.h"

extern vector<string> fileMap;

void ltrim(char *str)
{
    char *s = str;

    while (isspace(*s))
        s++;

    while ((*str++ = *s++));
}

void rtrim(char *str)
{
    char *s = str;
    register int i;

    for (i = strlen(s)-1; isspace(*(s+i)); i--)
        *(s+i) = '\0';

    while ((*str++ = *s++));
}


void trim(char *str)
{
    if(str == NULL) return ;

    ltrim(str);
    rtrim(str);
}

int strpos(char *haystack, char *needle)
{
    char *p = strstr(haystack, needle);
    if (p)
        return p - haystack;
    return -1;   // Not found = -1.
}

int strrpos(char *haystack, char needle)
{
    char *p = strrchr(haystack, needle);
    if (p)
        return p - haystack;
    return -1;   // Not found = -1.
}

int readFile(char *filename,char *key,char *value)
{
    char buffer[PRE_LINE];
    FILE *file = fopen(filename,"r");
    if (file == NULL)
    {
        printf("can not open %s",filename);
        return 0;
    }
    int klen = strlen(key);

    while(fgets(buffer,PRE_LINE,file)!=NULL)
    {
        char temp[50] = {'\0'};
        int j=klen+2;

        if(buffer[0]=='$' && buffer[klen+1] =='=')
        {
            strncpy(temp,&buffer[1],klen);
            temp[klen] = '\0';
            //printf("%s",key);
            if (strcmp(key,temp) == 0)
            {
                //排除掉换行符
                while(buffer[j] != '\0' && buffer[j]!=';' && buffer[j]!='\n')
                {
                    value[j-klen-2] = buffer[j];
                    j++;
                }
                value[j-klen-2] = '\0';
                break; //如果想得到最后一个的注释掉这一行
            }
        }
        else if(buffer[0] =='#'|| buffer[0] == '[')
        {
            continue;
        }
    }
    fclose(file);
    return 1;
}

int writeFile(char *filename,char * key ,char *value)
{
    FILE * file;
    file = fopen(filename,"a");
    if(file == NULL)
    {
        fclose(file);
        printf("can not open %s",filename);
        return 0;
    }

    if (fprintf(file,"$%s=%s\n",key,value) == 0)
    {
        return 1;
    }
    fclose(file);
    return 0;
}

int writeSection(char *filename,char* section)
{
    FILE * file;
    file = fopen(filename,"a");
    if(file == NULL)
    {
        fclose(file);
        printf("can not open %s",filename);
        return 0;
    }

    if (fprintf(file,"[%s]\n",section) == 0)
    {
        return 1;
    }
    fclose(file);
    return 0;

}

//把log目录的log文件读入vector中
void readAllFiles(char *pathName)
{
    DIR *dir;
    struct dirent *ptr;
    char host[100],username[100],pwd[100],db[50];
    dir = opendir(pathName);
    MYSQL mysql;
    mysql_init(&mysql);

    readFile(CONFIG,"host",host);
    readFile(CONFIG,"username",username);
    readFile(CONFIG,"password",pwd);
    readFile(CONFIG,"db",db);

    trim(host);
    trim(username);
    trim(pwd);
    trim(db);

    if(!mysql_real_connect(&mysql,host,username,pwd,db,0,NULL,0))
    {
        printf("Can't connect to mysqldb\n");
        return;
    }
    mysql_set_character_set(&mysql, "utf8");


    while((ptr=readdir(dir)) != NULL)
    {
        if (strpos(ptr->d_name,FILE_FIX) > -1 && !fileExist(&mysql,ptr->d_name)){
            fileMap.push_back(ptr->d_name);
            insertFile(&mysql,ptr->d_name);
        }
    }
    closedir(dir);
    mysql_close(&mysql);
    return;
}

//把文件行读入vector中
void fileToVector(const char *filename,std::vector<std::string>&vec){
    char buffer[PRE_LINE];
    FILE *file;

    file = fopen(filename,"rb");

    if (NULL == file)
    {
        //fclose(file);
        return;
    }

    while(fgets(buffer,PRE_LINE,file))
    {
        string t(buffer);
        vec.push_back(t);
    }
}

